package com.qf.ayc.user.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.ayc.orders.entity.Orders;
import com.qf.ayc.user.entity.Users;
import com.qf.ayc.user.mapper.UsersMapper;
import com.qf.ayc.user.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.ayc.user.vo.LoginUserVo;
import com.qf.ayc.user.vo.LoginVo;
import com.qf.ayc.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-02-20
 */
@Service
@Slf4j
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    @Autowired
    private UsersMapper userMapper;
    @Override
    public boolean regist(String username, String password, String nickname) {
        Users Auser = userMapper.selectOne(new QueryWrapper<Users>().eq("username", username));
        System.out.println(Auser+"Auser");
        if(Auser!=null){
            log.info("注册用户时用户名已存在，username={}", username);
            return false;
        }

        Users users = new Users();
        users.setUsername(username);

        String salt = UUID.randomUUID().toString().substring(0, 8);
        users.setSalt(salt);
        String md5Password = DigestUtil.md5Hex(password + salt);
        users.setPassword(md5Password);
        users.setNickname(nickname);
        userMapper.insert(users);
        return true;
    }

    @Override
    public LoginVo login(String username, String password) {
        Users Auser = userMapper.selectOne(new QueryWrapper<Users>().eq("username", username));
        LoginVo loginVo = new LoginVo();
        if(Auser==null){
            log.info("没有找到对应的用户信息，用户名{}错误",username);
            loginVo.setCode(-1);
            loginVo.setMsg("没有找到对应的用户信息，用户名错误");
            return loginVo;
        }
        String salt = Auser.getSalt();
        String dbpassword = Auser.getPassword();
        String md5password = DigestUtil.md5Hex(password + salt);
        if(!dbpassword.equals(md5password)){
            log.info("用户名或密码错误");
            loginVo.setCode(-2);
            loginVo.setMsg("用户名或密码错误");
            return loginVo;
        }
        String nickname = Auser.getNickname();
        String attr = Auser.getAttr();
        String token = JwtUtils.createToken(username, 120);
        String checkedkeys = Auser.getCheckedkeys();
        Integer id = Auser.getId();
//        ArrayList<String> list = new ArrayList<>();
//        list.add(checkedkeys);
        loginVo.setMsg("用户登录成功");
        loginVo.setCode(1);
        loginVo.setLoginUserVo(new LoginUserVo(username,token,attr,nickname,checkedkeys,id));
        return loginVo;
    }

    @Override
    public List<Users> getlist(Integer pageNum, Integer limit) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        Page<Users> page = new Page<>(pageNum,limit);
        userMapper.selectPage(page, queryWrapper);
        List<Users> records = page.getRecords();
        return records;
    }

    @Override
    public List<Users> List() {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        List<Users> users = userMapper.selectList(queryWrapper);
        return users;
    }
}
